package com.cd.socialMarketing.web.seguridad;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.DefaultSavedRequest;

//import com.cd.socialMarketing.model.Usuario;
import com.cd.socialMarketing.service.generic.IGenericManager;

public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
	
//	@Autowired
//	@Qualifier(value="usuarioManager")
//	private IGenericManager<Long, Usuario> usuarioManager;
	
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
	
//		Map<String, Object> usuarioFilter = new HashMap<String, Object>();
//		usuarioFilter.put("mail", authentication.getName());
//        request.getSession().setAttribute("usuario", usuarioManager.findUnique(usuarioFilter));

        DefaultSavedRequest defaultSavedRequest = (DefaultSavedRequest) request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST");
        if( defaultSavedRequest == null || defaultSavedRequest.getRequestURI().indexOf("none")!=-1 ) {
            getRedirectStrategy().sendRedirect(request, response, "/index.htm");
        } else {
            super.onAuthenticationSuccess(request, response, authentication);
        }
	}

}
